<?

//Data loading
$title='LEDGER';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
$Result = GridList ("", "", "");
if ($Result) $pdf->GridTable($Result);
$pdf->Output();

function GetFromDate () {
    if (GetParam("m","")=="AUDIT")
    list($Day,$Month,$Year,$NMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,12,1,GetParam("y",""))));
    else
    list($Day,$Month,$Year,$NMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,GetParam("m",""),1,GetParam("y",""))));
    return array($Day,$Month,$Year,$NMonth);
}

function GetLastPeriod ($Month, $Year) {
    list($LDay,$LMonth,$LYear,$LNMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,$Month,0,$Year)));
    return array($LMonth,$LYear,$LNMonth);
}

function GridList ($Space, $Index, $ParentID) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

            list($Day,$Month,$Year,$NMonth) = GetFromDate();

            if ($Month<10) $Month = "0".$Month;
            if (($Month-1)<10) $LastMonth = "0".($Month-1);

            if ($Month<2) $LastMonth = "awal";
            else {$LastMonth = "aku".$LastMonth;}

            if (GetParam("f_c",""))
//die('GetParam("f_c","")'.GetParam("f_c",""));
                $WHERE = "  ais_memorial.skada='".GetParam("f_c","")."' ";
            if (GetParam("m","") && GetParam("y",""))
                if (strlen($WHERE)) $WHERE .= " AND MONTH(tglbkt)='".GetParam("m","")."' AND YEAR(tglbkt)='".GetParam("y","")."'";
                else $WHERE .= " MONTH(tglbkt)='".GetParam("m","")."' AND YEAR(tglbkt)='".GetParam("y","")."'";
            if (GetParam("o",""))
                if (strlen($WHERE)) $WHERE .= " AND ais_memorial.ktrasl='".GetParam("o","")."'";
                else $WHERE .= " ais_memorial.ktrasl='".GetParam("o","")."'";

            $SQL  = "
            SELECT ais_memorial.kdact,ais_memorial.debtur,ais_memorial.noref,ais_memorial.rate,ais_memorial.ktrasl,ais_memorial.nobkt,ais_memorial.tglbkt,ais_memorial.skada,ais_memorial.uraian,ais_memorial.dk,ais_memorial.rupiah,$LastMonth as lastmonth,ais_memorial.curr, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate
            FROM ais_memorial INNER JOIN tblcoa ON tblcoa.skada=ais_memorial.skada
            ";

            if (strlen($WHERE)) $SQL .= " WHERE ".$WHERE;
            $SQL .= " ORDER BY ais_memorial.ktrasl, ais_memorial.SKADA, ais_memorial.nobkt";
            //print $SQL;die();
            //"WHERE skada='".GetParam("c","")."'";
            $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
            return $MyResult;
}

function GetSaldoAwal($Month,$year) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            if ($Month<10) $Month = "0".$Month;
            if (($Month-1)<10) $LastMonth = "0".($Month-1);

            if ($Month<2) $LastMonth = "awal";
            else {$LastMonth = "aku".$LastMonth;}

            $SQL = "SELECT $LastMonth as lastmonth FROM tblcoa WHERE ledger='$year' ";
            if (GetParam("o","")) $SQL .= " ktrasl='".GetParam("ktrasl","")."'";

            //print $SQL;die();

            return $DBConnection->dbc->get_var($SQL);
}

function ReadCOA($no) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            $SQL = "SELECT namacc FROM tblcoa WHERE skada='".$no."'";
            $MyResult = $DBConnection->dbc->get_var($SQL);
            return $MyResult;
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}

class PDFReport extends PDF {

  function PDFReport($Page,$Size,$Mode) {
    parent::PDF($Page,$Size,$Mode);
        $this->AddFont('arialn','','arialn.php');
        $this->SetLeftMargin(5);
  }

    function Header() {
        global $title,$perush,$page,$ACCOUNT;

    if (!strlen($ACCOUNT)) $ACCOUNT = GetParam("f_c","");

        $this->SetFont('arialn','',10);
        $this->Cell(180,5,$perush,0,1,'C',0);
        $this->Cell(180,5,"LEDGER PERIOD: ".BuildPeriod (GetParam("y",""), GetParam("m","")),0,1,'C',0);
        $this->Cell(180,5,"REPORT DATE: ".date("d/m/y"),0,1,'C',0);
if (trim($ACCOUNT)=='') {
        $this->Cell(90,5,"ACCOUNT : 00001000 - ".ReadCOA('00001000'),0,0,'L',0);
}
else {
        $this->Cell(90,5,"ACCOUNT : ".$ACCOUNT." - ".ReadCOA($ACCOUNT),0,0,'L',0);
}
        $this->Cell(110,5,$page." ".$this->PageNo(),0,1,'R',0);
//        $this->Line(5,30,210,30);
//        $this->Ln();
        //Header

        //Column widths

        $w=array(10,20,10,10,15,20,15,40,10,15,20,20);
        $this->SetFont('arialn','',8);
        $this->Cell($w[0],5,"NO.",1,0,'C',0);
        $this->Cell($w[1],5,"TRANS. NO",1,0,'C',0);
        $this->Cell($w[2],5,"KTR",1,0,'C',0);
        $this->Cell($w[3],5,"UU",1,0,'C',0);
        $this->Cell($w[4],5,"DATE",1,0,'C',0);
        $this->Cell($w[5],5,"REF. NO",1,0,'C',0);
        $this->Cell($w[6],5,"DEBTUR",1,0,'C',0);
        $this->Cell($w[7],5,"DESCRIPTION",1,0,'C',0);
        $this->Cell($w[8],5,"CUR",1,0,'C',0);
        $this->Cell($w[9],5,"RATE",1,0,'C',0);
        $this->Cell($w[10],5,"DEBET",1,0,'C',0);
        $this->Cell($w[11],5,"CREDIT",1,0,'C',0);
        $this->Ln();
    }

    function GridTable($MyResult) {
        global $ACCOUNT;
        //Column widths
        $w=array(10,20,10,10,15,20,15,40,10,15,20,20);

        //Data


        foreach($MyResult as $row)
        {
            if ($row["dk"]=='D') $row["rupiah1"] = $row["rupiah"];
            else $row["rupiah2"] = $row["rupiah"];
            $totald = $totald + $row["rupiah1"];
            $totalc = $totalc + $row["rupiah2"];

            if (($skada<>$row["skada"]) && (strlen($skada)>0)) {
        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"SUB TOTAL:",1,0,'R',0);
        $this->Cell($w[10],5,number_format($subtotald,2),1,0,'R',0);
        $this->Cell($w[11],5,number_format($subtotalc,2),1,0,'R',0);
        $this->Ln();


                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"TOTAL:",1,0,'R',0);
                $this->Cell($w[10],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
            $this->Cell($w[11],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
            $this->Ln();
                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"SALDO AKHIR:",1,0,'R',0);
                $Balance = ($subtotald+$SaldoDebet)-($subtotalc+$SaldoKredit);
            if ($Balance>0) {
                $this->Cell($w[10],5,number_format($Balance,2),1,0,'R',0);
                $this->Cell($w[11],5,"",1,0,'R',0);
            } else {
                $this->Cell($w[10],5,"",1,0,'R',0);
                $this->Cell($w[11],5,number_format($Balance*-1,2),1,0,'R',0);

            }
            $this->Ln();
            $ACCOUNT = $row["skada"];
            $this->AddPage();

                $subtotald = 0;
                $subtotalc = 0;
            }

            //*********** SALDO AWAL *************
            if (($skada<>$row["skada"])) {
                $Saldo = $row["lastmonth"];
                if ($Saldo>0) $SaldoDebet = $Saldo;
                else {$SaldoKredit = $Saldo; $SaldoKredit=$SaldoKredit*-1;}
                $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"SALDO AWAL: ",'BTLR',0,'R');
                $this->Cell($w[10],5,number_format($SaldoDebet,2),'LR',0,'R');
          $this->Cell($w[11],5,number_format($SaldoKredit*-1,2),'LR',0,'R');
                $this->Ln();
            }
            //*********** SALDO AWAL *************

            $subtotald = $subtotald + $row["rupiah1"];
            $subtotalc = $subtotalc + $row["rupiah2"];

          $n++;
            $this->Cell($w[0],3,$n,'LR',0,'R');
            $this->Cell($w[1],3,$row["nobkt"],'LR');
          $this->Cell($w[2],3,$row["ktrasl"],'LR',0,'C');
          $this->Cell($w[3],3,$row["kdact"],'LR',0,'C');

            $this->Cell($w[4],3,$row["trdate"],'LR',0,'C');
            $this->Cell($w[5],3,$row["noref"],'LR');
            $this->Cell($w[6],3,$row["debtur"],'LR',0,'R');
            $this->Cell($w[7],3,substr($row["uraian"],0,26),'LR');
          $this->Cell($w[8],3,$row["curr"],'LR',0,'C');
          $this->Cell($w[9],3,FormatNumber($row["rate"],2),'LR',0,'R');
            $this->Cell($w[10],3,FormatNumber($row["rupiah1"],2),'LR',0,'R');
             $this->Cell($w[11],3,FormatNumber($row["rupiah2"],2),'LR',0,'R');
            $this->Ln();
            $skada = $row["skada"];
        } // foreach

        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"SUB TOTAL:",1,0,'R',0);
        $this->Cell($w[10],5,number_format($subtotald,2),1,0,'R',0);
        $this->Cell($w[11],5,number_format($subtotalc,2),1,0,'R',0);
        $this->Ln();

        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"TOTAL:",1,0,'R',0);
        $this->Cell($w[10],5,number_format($subtotald+$SaldoDebet,2),1,0,'R',0);
        $this->Cell($w[11],5,number_format($subtotalc+$SaldoKredit,2),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4]+$w[5]+$w[6]+$w[7]+$w[8]+$w[9],5,"SALDO AKHIR:",1,0,'R',0);
        $Balance = ($subtotald+$SaldoDebet)-($subtotalc+$SaldoKredit);
            if ($Balance>0) {
                $this->Cell($w[10],5,number_format($Balance,2),1,0,'R',0);
                $this->Cell($w[11],5,"",1,0,'R',0);
            } else {
                $this->Cell($w[10],5,"",1,0,'R',0);
                $this->Cell($w[11],5,number_format($Balance*-1,2),1,0,'R',0);

            }
$this->Ln();
/*
        $this->Cell(130,5,"TOTAL:",1,0,'R',0);
        $this->Cell(25,5,number_format($totald,2),1,0,'R',0);
        $this->Cell(25,5,number_format($totalc,2),1,0,'R',0);
        $this->Ln();
*/
        //Closure line
    }

    function Footer() {
        //Closure line
        $this->Cell(200,0,'','T');
    }

}


?>
